//
//  ViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/1.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "ViewController.h"
#import "Theme-PrefixHeader.pch"
#import "SAPWeatherViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, newestDelegate, newsFlashDelegate, videoDelegate, originalDelegate, speakerDelegate, baseDelegate>
@property (nonatomic, retain) UICollectionView     *collectionView;

@property (nonatomic, retain) NSMutableArray       *newestDataSource;
@property (nonatomic, retain) NSMutableArray       *buttonArray;
@property (nonatomic, retain) UIScrollView         *scrollView;
@property (nonatomic, retain) ColumnViewController *columnView;
/**
 *  all model
 */
@property (nonatomic, retain) recommentOneModel     *oneModel;
@property (nonatomic, retain) flashOneModel         *flashModel;
@property (nonatomic, retain) SAPVideoOneModel      *videoModel;
@property (nonatomic, retain) SAPpriceOneModel      *priceModel;
@property (nonatomic, retain) SAPUseCarOneModel     *useCarModel;
@property (nonatomic, retain) SAPTechnologyOneModel *technologyModel;
@property (nonatomic, retain) SAPOriginalOneModel   *originalModel;
@property (nonatomic, retain) SAPSpeakerOneModel    *speakerModel;
@property (nonatomic, retain) SAPCarTuningOneModel  *carTuningModel;
@property (nonatomic, retain) SAPConsulOneModel     *consulModel;
@property (nonatomic, retain) SAPTravelOneModel     *travelModel;
@property (nonatomic, retain) SAPEvaluationOneModel *evaluationModel;
@property (nonatomic, retain) SAPNewsOneModel       *newsModel;
@property (nonatomic, retain) SAPCultureOneModel    *cultureModel;

@end

@implementation ViewController
- (void)dealloc {
    [_collectionView release];
    [_newestDataSource release];
    [_scrollView release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self collectionViews];
    [self weatherButton];
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    _scrollView.contentSize = CGSizeMake(SCREEN_SIZE.width * 2 + 100, 44);
    self.navigationItem.titleView = self.scrollView;
    self.navigationController.navigationBar.translucent = NO;
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(toCuclumView:)];
   
    [self button];
    
}
- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
        self.tabBarController.tabBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tuijianNav.jpg"] forBarMetrics:UIBarMetricsDefault];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    }
    [self.collectionView reloadData];
    
}




#pragma mark - collectionDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   
    return 13;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        RecommentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommentCollectionViewCellINdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 1) {
        newsFlashCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newsFlashCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 2) {
        SAPVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPVideoCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
//    else if (indexPath.item == 3) {
//        SAPPriceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPPriceCollectionViewCellIndentifier" forIndexPath:indexPath];
//        cell.delegate = self;
//        return cell;
//    }
    else if (indexPath.item == 3) {
        SAPUseTheCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPUseTheCarCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 4) {
        SAPTechnologCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPTechnologCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 5) {
        SAPOriginalVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPOriginalVideoCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 6) {
        SAPSpeakerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPSpeakerCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.oneModel = self.speakerModel;
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 7) {
        SAPCarTuningCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPCarTuningCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 8) {
        SAPConsultingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPConsultingCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 9) {
        SAPTravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPTravelCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 10) {
        SAPEvaluationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPEvaluationCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 11) {
        SAPNewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPNewsCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.item == 12) {
        SAPCultureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPCultureCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    
}
    
    return nil;
}
- (void)collectionViews {
 
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 44);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;

        [_collectionView registerClass:[RecommentCollectionViewCell class] forCellWithReuseIdentifier:@"recommentCollectionViewCellINdentifier"];
        [_collectionView registerClass:[newsFlashCollectionViewCell class] forCellWithReuseIdentifier:@"newsFlashCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPVideoCollectionViewCell class] forCellWithReuseIdentifier:@"SAPVideoCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPPriceCollectionViewCell class] forCellWithReuseIdentifier:@"SAPPriceCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPUseTheCarCollectionViewCell class] forCellWithReuseIdentifier:@"SAPUseTheCarCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPTechnologCollectionViewCell class] forCellWithReuseIdentifier:@"SAPTechnologCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPOriginalVideoCollectionViewCell class] forCellWithReuseIdentifier:@"SAPOriginalVideoCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPSpeakerCollectionViewCell class] forCellWithReuseIdentifier:@"SAPSpeakerCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPCarTuningCollectionViewCell class] forCellWithReuseIdentifier:@"SAPCarTuningCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPConsultingCollectionViewCell class] forCellWithReuseIdentifier:@"SAPConsultingCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPTravelCollectionViewCell class] forCellWithReuseIdentifier:@"SAPTravelCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPEvaluationCollectionViewCell class] forCellWithReuseIdentifier:@"SAPEvaluationCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPNewsCollectionViewCell class] forCellWithReuseIdentifier:@"SAPNewsCollectionViewCellIndentifier"];
        [_collectionView registerClass:[SAPCultureCollectionViewCell class] forCellWithReuseIdentifier:@"SAPCultureCollectionViewCellIndentifier"];
  
}
#pragma mark - topButton
//- (void)insertToOptional {
//    
//    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"最新", @"快报", @"视频", @"行情", @"用车", @"技术", @"原创视频", @"说客", @"改装", @"导购", @"游记", @"测评", @"新闻", @"文化", nil];
//    SAKeyValueStore *valueStore = [[SAKeyValueStore alloc] initDBWithName:@"coclumn.db"];
//    NSString *tableName = @"coclumn";
//    [valueStore createTableWithName:tableName];
//    
//    [valueStore putObject:array withId:@"ids" intoTable:tableName];
//    [valueStore getAllItemsFromTable:tableName];
//    for (SAKeyValueItem *item in [valueStore getAllItemsFromTable:tableName]) {
//        [self.newestDataSource addObjectsFromArray:item.itemObject];
//        [self button];
//    }
//    
//    
//    
//}
- (void)button {
    self.buttonArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    SAKeyValueStore *selectedStore = [[SAKeyValueStore alloc] initDBWithName:@"selected.db"];
    NSString *tableName = @"selectd";
    [selectedStore createTableWithName:tableName];
    self.newestDataSource = [[NSMutableArray alloc] init];
    [selectedStore getAllItemsFromTable:tableName];
    for (SAKeyValueItem *item in [selectedStore getAllItemsFromTable:tableName]) {
        [self.newestDataSource addObjectsFromArray:item.itemObject];
        
    }
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"最新", @"快报", @"视频", @"用车", @"技术", @"原创视频", @"说客", @"改装", @"导购", @"游记", @"测评", @"新闻", @"文化", nil];
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self action:@selector(buttonForCollectionView:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(60 * i, 0, 60, 40);
        button.tag = i + 100;
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (i == 0) {
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        }
        [self.scrollView addSubview:button];
        [self.buttonArray addObject:button];
    }
}

- (void)buttonForCollectionView:(UIButton *)sender {
    [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    for (int i = 0; i < self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        if (sender.tag != button.tag) {
            [self.buttonArray[i] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }

    
    [self.collectionView setContentOffset:CGPointMake((sender.tag - 100) * self.view.frame.size.width, 0)];
    [self.collectionView reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int temp = self.collectionView.contentOffset.x / SCREEN_SIZE.width;
    UIButton *button = self.buttonArray[temp];

    if (temp  > 2 && temp < self.buttonArray.count) {
        self.scrollView.contentOffset = CGPointMake((temp - 2)* 60, 0);
        
    } else if (temp > self.buttonArray.count - 3) {
        self.scrollView.contentOffset = CGPointMake(15 * 60, 0);
    } else {
        self.scrollView.contentOffset= CGPointMake( 0, 0);
    }
    [self buttonForCollectionView:button];
}
#pragma mark - toColumnView && backSelfView
- (void)toCuclumView:(UIBarButtonItem *)sender {
    self.columnView = [[ColumnViewController alloc] init];
    self.columnView.title = self.title;
//    [self.columnView.optionalArray addObjectsFromArray:self.newestDataSource];
    _columnView.view.frame = CGRectMake(0, 5, self.view.frame.size.width, self.view.frame.size.height);
    self.navigationController.navigationBarHidden = YES;
    
//    [self.navigationController pushViewController:self.columnView animated:YES];
    [self.view addSubview:self.columnView.view];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, SCREEN_SIZE.height / 2, SCREEN_SIZE.width, SCREEN_SIZE.height / 2);
    [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.columnView.view addSubview:button];
    
}

- (void)buttonDidPress:(UIButton *)sender {
    self.columnView.view.frame = CGRectMake(0, - SCREEN_SIZE.height, 0, 0);
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - webViewDelegate

- (void)passValue:(UIWebView *)web withUrl:(NSString *)url {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    web.frame = CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height);
    [webView.view addSubview:web];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passSPeakerValue:(NSString *)url {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passOriginalValue:(NSString *)url {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passBaseValue:(NSString *)url {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passFlashValue:(NSString *)url {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)passVideoValue:(NSString *)string {
    SAPWebViewController *webView = [[SAPWebViewController alloc] init];
    webView.url = string;
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)weatherButton {
    UIImageView *weather = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 50, 50)];
    weather.image = [UIImage imageNamed:@"10.png"];
    weather.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(weatherButtonAction:)];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapclick4:)];
    [weather addGestureRecognizer:pan];
    [weather addGestureRecognizer:tap];
    [self.collectionView addSubview:weather];
}
- (void)weatherButtonAction:(UITapGestureRecognizer *)weathers {
    
    SAPWeatherViewController *weatherVC = [[SAPWeatherViewController alloc] init];
    
    [self.navigationController pushViewController:weatherVC animated:YES];
}
- (void)tapclick4:(UIPanGestureRecognizer *)pan {
    UIView *imageV = pan.view;
    CGPoint point = [pan translationInView:imageV];
    imageV.transform = CGAffineTransformTranslate(imageV.transform, point.x, point.y);
    [pan setTranslation:CGPointZero inView:imageV];
}
- (void)viewWillAppear:(BOOL)animated {
    [self changeColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
